package EWCore.Items.Attributes
{
	import EWCore.Utilities.Useful;
	public class Attribute
	{
		public var m_Value:Number;
		public var m_AttributeType:AttributeType;
		public function Attribute()
		{
			
		}
		/**
		 * 按简称创建指定属性
		 * @param i_ShortName 属性类型简称
		 * @param i_Level 物品等级
		 * @param i_Quality 属性品质
		 * */
		public static function CreateAttribute(i_ShortName:String,i_Level:Number,i_Quality:Number):Attribute
		{
			var t_AttributeType = AttributeType.GetTypeByName(i_ShortName);
			if(t_AttributeType==null)
				return null;
			var t_Attribute:Attribute = new Attribute();
			t_Attribute.m_AttributeType = t_AttributeType;
			t_Attribute.m_Value = CalcAttributeValue(t_AttributeType.expression,i_Level,i_Quality);
			return t_Attribute;
		}
		
		/**
		 * 按分类创建随机属性
		 * @param i_Type 物品类型
		 * @param i_Level 物品等级
		 * @param i_Quality 属性品质
		 * @param i_ClacQuality 物品品质
		 * */
		public static function CreateAttributeByType(i_Type:String,i_Level:Number,i_Quality:Number,i_ClacQuality:Number):Attribute
		{
			var t_AttributeType = AttributeType.GetTypeRand(i_Quality,i_Type);
			if(t_AttributeType==null)
				return null;
			var t_Attribute:Attribute = new Attribute();
			t_Attribute.m_AttributeType = t_AttributeType;
			t_Attribute.m_Value = CalcAttributeValue(t_AttributeType.expression,i_Level,i_ClacQuality);
			
			return t_Attribute;
		}
		/**
		 * 计算属性值
		 * @param expression 计算公式
		 * @param i_Level 物品等级
		 * @param i_Quality 物品品质
		 * */
		public static function CalcAttributeValue(expression:String,i_Level:Number,i_Quality:Number)
		{
			return Useful.CalcExpression(expression,["LV","PZ"],[i_Level,i_Quality]);
		}
		public function toString():String
		{
			var valstyle1:Number = (m_Value-1)*100;
			var valstr1:String=valstyle1.toFixed(2).toString()+"%";
			if(valstyle1>0)
				valstr1 = "+"+valstr1;
			
			var valstyle2:Number = m_Value;
			var valstr2:String=valstyle2.toFixed(2).toString();
			if(valstyle2>0)
				valstr2 = "+"+valstr2;
			return m_AttributeType.fullname.replace("$0%",valstr1).replace("$0",valstr2)+" 公式："+this.m_AttributeType.expression+"\n";
		}
	}
}